package com.myutils;

import java.io.File;

import org.apache.log4j.Logger;
import org.dom4j.Document;

import com.opensymphony.oscache.base.NeedsRefreshException;

/**
 * @author Lan
 * @date 2012-7-17
 */
public class CacheUtil {

	private static Logger logger = Logger.getLogger(CacheUtil.class);
	
	private static CacheManager cacheManager = CacheManager.getInstance();
	
	public static Document getXml(String documentKey, String fileKey){
		Document document = null;
		if(cacheManager.isFileModified(fileKey)){
			logger.debug("重新获取本地文件" + fileKey + "的Document，并更新缓存");
			document = XmlUtil.getXmlDocument(new File(fileKey));
			cacheManager.putInCache(documentKey, document);//更新缓存
		}else{
			try {
				logger.debug("从缓存中读取key=" + documentKey + "的Document");
				document = (Document) cacheManager.get(documentKey);
			} catch (NeedsRefreshException e) {
				logger.error("重新从本地读取" + fileKey + "并更新缓存");
				document = XmlUtil.getXmlDocument(new File(fileKey));
				cacheManager.putInCache(documentKey, document);//更新缓存
			}
		}
		return document;
	}
	
}
